package com.kaibes.module.message;

import java.math.BigInteger;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/messages")
public class MessageApi extends ResponseApi<Message, MessageService> {

    @ApiLink("给所有用户发送消息")
    @PostMapping("/send/all")
    public ResponseEntity<List<Number>> sendAll(@RequestAttribute Integer userId, @RequestBody MessagePostAll message) {
        MessageInsert msg = new MessageInsert();
        msg.setTitle(message.getTitle());
        msg.setContent(message.getContent());
        msg.setHtml(message.getHtml());
        msg.setFromId(userId);
        return post(msg, "发送消息成功", "发送消息失败");
    }

    @PostMapping("/send")
    @ApiLink("发送消息")
    public ResponseEntity<List<Number>> send(@RequestAttribute Integer userId, @RequestBody MessagePost message) {
        MessageInsert msg = new MessageInsert();
        msg.setTitle(message.getTitle());
        msg.setContent(message.getContent());
        msg.setHtml(message.getHtml());
        msg.setFromId(userId);
        msg.setToId(message.getToId());
        return post(msg, "发送消息成功", "发送消息失败");
    }
    
    @DeleteMapping("/{id}")
    @ApiLink("删除消息")
    public ResponseEntity<Object> delete(@PathVariable BigInteger id) {
        return super.deleteById(id, "删除消息成功", "删除消息失败");
    }
    
    @DeleteMapping("/self")
    @ApiLink(value = "删除所有消息", validated = false)
    public ResponseEntity<Object> deleteSelf(@RequestAttribute Integer userId) {
        return ResponseFactory.delete(service.removeByToId(userId), "删除消息成功", "删除消息失败");
    }
    
    @DeleteMapping("/self/{id}")
    @ApiLink(value = "删除消息", validated = false)
    public ResponseEntity<Object> deleteSelf(@RequestAttribute Integer userId, @PathVariable BigInteger id) {
        return ResponseFactory.delete(service.removeByIdAndToId(id, userId), "删除消息成功", "删除消息失败");
    }
}
